package fr.cohen.image.simple;

import java.awt.Image;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import fr.cohen.image.CohenImage;

public class CohenImageIcon implements CohenImage {

	private ImageIcon image;
	private String name;

	/**
	 * @param image
	 * @param name
	 */
	public CohenImageIcon(String name, ImageIcon image) {
		this.image = image;
		this.name = name;
	}

	public Icon getIcon() {
		return image;
	}

	public String getName() {
		return name;
	}

	public Image getImage() {
		return image.getImage();
	}

	public void resize(int maxWidth, int maxHeight) {

		int width = image.getIconWidth();
		int height = image.getIconHeight();

		double coeffW = (double)maxWidth / (double)width;
		double coeffH = (double)maxHeight / (double)height;
		double coeff = Math.min(coeffW, coeffH);

		this.image = new ImageIcon(
			this.image.getImage().getScaledInstance(
				(int)(coeff * width),
				(int)(coeff * height),
				Image.SCALE_SMOOTH
			)
		);
	}

	@Override
	public boolean equals(Object object) {

		if(object == null){

			return false;
		}
		else{

			if(object instanceof CohenImage){

				CohenImage other = (CohenImage)object;
				return(this.getName().equals(other.getName()));
			}
			else{

				return false;
			}
		}
	}

	@Override
	public int hashCode() {

		return (name == null ? 0 : name.hashCode());
	}

	@Override
	public String toString() {

		return name;
	}
}
